<template>
	<gui-page>
		<template v-slot:gBody>
			<view style="padding:50rpx;">
				<view style="height:150rpx;"></view>
				<view class="gui-flex gui-row gui-justify-content-center">
					<text 
					class="login-logo gui-icons gui-color-white gui-block gui-border-box gui-bg-primary gui-box-shadow gui-box">&#xe620;</text>
				</view>
				<view 
				style="margin-top:80rpx;">
					<form @submit="submit">
						<view>
							<text class="gui-text-small gui-color-gray">账户</text>
						</view>
						<view class="gui-border-b">
							<input 
							type="text" 
							class="gui-form-input" 
							name="username" 
							placeholder="登录账户" />
						</view>
						<view class="gui-margin-top">
							<text 
							class="gui-text-small gui-color-gray">密码</text>
						</view>
						<view class="gui-border-b">
							<input 
							type="password" 
							class="gui-form-input" 
							name="password" 
							placeholder="密码" />
						</view>
						
						
						<view style="margin-top:38rpx;">
							<button 
							type="default" 
							class="gui-button gui-bg-primary gui-noborder" 
							formType="submit" 
							style="border-radius:50rpx;">
								<text 
								class="gui-color-white gui-button-text">登 录</text>
							</button>
						</view>
					</form>
				</view>
				
				
			</view>
		</template>
	</gui-page>
</template>
<script>
import useUser from "@/api/user";
import graceChecker from "@/Grace6/js/checker.js";
const {userLogin} = useUser();
export default {
	data() {
		return {}
	},
	methods: {
		forgetPwd  : function () { console.log('请自行完善代码'); },
		loginbymsg : function () { console.log('请自行完善代码'); },
		submit : async function(e){
			// 表单数据
			var formData = e.detail.value;
			console.log(formData);
			// 利用 graceUI 的表单验证工具进行验证
			//定义表单规则
			var rule = [
				{name:"username", checkType : "string", checkRule:"1,50", errorMsg:"登录账户输入有误"},
				{name:"password", checkType : "string", checkRule:"6,100", errorMsg:"登录密码至少6个字符"},
			];
			var checkRes = graceChecker.check(formData, rule);
			if(checkRes){ 
				uni.showToast({title:"验证通过!", icon:"none"});
				// 根据项目需要完成数据验证工作
				const {data} = await userLogin(formData)
				let user = data.user
				uni.setStorageSync("user",JSON.stringify(user))
				uni.setStorageSync("token",data.token)
				uni.setStorageSync("roles",JSON.stringify(data.user.roles))
				uni.reLaunch({
					url:`/pages/index/index`
				})
			}else{
				uni.showToast({ title:graceChecker.error, icon: "none" });
			}
		}
	}
}
</script>
<style scoped>
.login-logo{width:180rpx; height:180rpx; font-size:80rpx; text-align:center; line-height:120rpx; padding:30rpx; border-radius:18rpx;}
.other-login-icons{width:88rpx; height:88rpx; text-align:center; font-size:70rpx; margin:20rpx;}
</style>